package com.kitking.app3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText editText;
    TextView textView;
    //自定义广播测试
    //1，要注册的自定义广播 广播
    public static final String MY_ACTION="com.example.broadcasttest.MY_BROADCAST";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle(getPackageName());
        editText=findViewById(R.id.ed1);
        textView=findViewById(R.id.tv1);
        //二：注册广播
        //1,为广播接收器添加action
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(MY_ACTION);
        //2,注册广播
        MyBroadcastReceiver myBroadcastReceiver=new MyBroadcastReceiver(textView);
        //3,谁去接收呢？就是你动态注册的那个类去接收
        registerReceiver(myBroadcastReceiver,intentFilter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bun1:
             /*   //注册广播，发送广播
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
                //安卓8.0以后静态广播都需要setComponent,两个参数一个具体指定类名，一个具体指定包名
                intent.setComponent(new ComponentName(getPackageName(),getPackageName()+".MyBroadcastReceiver"));
                // intent.setComponent(new ComponentName(getPackageName(),getPackageName()+".AnotherBroadcastReceiver"));
                sendBroadcast(intent);
                Log.d("Tag------","测试广播。。。。。。。。。。"+"com.example.broadcasttest.MY_BROADCAST");
*/
                //一：发送广播
                //1,新建广播
                Intent intent=new Intent(MY_ACTION);
                //2,放入广播携带的数据
                intent.putExtra("broadcastcontent",editText.getText().toString());
                //3,发送广播及其携带的数据
                sendBroadcast(intent);

                break;
            default:
                Log.d("Tag------","测试广播失败啦~。。。。。。。。。。");
        }
    }
}